<head>
<title>用户登陆页面</title>
<style type="text/css">
body {
	background-image: url(background1.jpg);
}
</style>
</head>

<body>
<p style="text-align:center;font-family: sans-serif;font-size:20px;color:blue;left:0; top:13px;">
欢迎来到moodle API 简单面板
<br>
请登陆/注册</p>
<form action="login-c.php" method = "POST">
<div align="center">
<table width="355" border="1">
  <tr>
    <td width="163">用户名：</td>
    <td width="176"><input align = "center" type="text" name = "username"value = ""></td>
  </tr>
    <tr>
    <td>密&nbsp;&nbsp;&nbsp;&nbsp;码：</td>
    <td><input align = "center" type="password" name = "password" value = ""></td>
  </tr>
  <tr>
    <td width="163">姓（注册必填）：</td>
    <td width="176"><input align = "center" type="text" name = "firstname"value = ""></td>
  </tr>
  <tr>
    <td width="163">名（注册必填）：</td>
    <td width="176"><input align = "center" type="text" name = "lastname"value = ""></td>
  </tr>
   <tr>
    <td width="163">邮箱（注册必填）：</td>
    <td width="176"><input align = "center" type="text" name = "email"value = ""></td>
  </tr>
   <tr>
    <td width="163">身份（注册必填）：</td>
    <td width="176">教师<input align = "center" type="radio" name = "identity"value = "teacher">&nbsp;&nbsp;学生<input align = "center" type="radio" name = "identity"value = "student"></td>
  </tr>
  <tr>
    <td colspan="2"> <div align="center">
      <input align = "center" type="submit" name="button1" value="登陆" />
      <input align = "center" type="submit" name="button2" value="注册" />
    </div></td>
    </tr>
</table>
</div>
</form>

<?php
/*---------------------一、变量参数定义-------------------------------*/
$button1 = $_POST['button1'];
$button2 = $_POST['button2'];
if($button1 || $button2)
{
	$username = $_POST['username'];
	$password = $_POST['password'];
	$email = $_POST['email'];
	$identity = $_POST['identity'];
	$firstname = $_POST['firstname'];
	$lastname = $_POST['lastname'];
	if(empty($username) || empty($password)) 
	{
		die('用户名/密码不能为空');
	}
	if($button1)
	{
		echo "login<br>";	
		$show = 1;
	}
	else if($button2)
	{
		echo "register<br>";	
		$show = 2;
	}
	//设置并发送变量
	$url = 'http://172.30.5.116/api_moodle/api_login.php';
	//$urrl ='api_login.php';
	$fields = array('username'=>urlencode($username),'password'=>urlencode($password),'email'=>urlencode($email),'identity'=>urlencode($identity),'firstname'=>urlencode($firstname),'lastname'=>urlencode($lastname),'show'=>urlencode($show));
	foreach($fields as $key=>$value)
	{
		$fields_string .= $key.'='.$value.'&' ;
	}
	rtrim($fields_string ,'&') ; //从末端开始去掉删除多余&		
	$ch = curl_init() ; // 开启一个curl对话
	curl_setopt($ch, CURLOPT_URL,$url); //把要对话的url地址设置好
	//curl_setopt($ch, CURLOPT_POST,count($fields)) ; //把要发送的变量数量设置好
	curl_setopt($ch,CURLOPT_POST,1);
	curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); //把要post的变量url组装好化后设置好 
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	curl_exec($ch); //执行上面设置好的一个curl操作，把变量数据发送完后，返回的是获得post变量执行后的结果
	$result = curl_multi_getcontent($ch);
	curl_close($ch) ; //关闭curl对话
	$result = htmlspecialchars_decode($result); 
	json_decode($result,true);
	/*测试数据*/
	//echo "<br>";
	var_dump($result);	
	//echo $errorinfo = json_last_error();
	
	if($show == 1)
	{
		if(strlen($result)<80) //刷新页面
		{
			echo 'login failed';
			//sleep(5);
			//echo "<script>";
			//echo "location.reload()";
           /* echo "</script>";	*/
		}
		else //跳转页面
		{
//			echo '<br>login success<br>';	
//			$url = "http://172.30.5.116/api_moodle/admin-c.php?username=$username";
			$url = "admin-c.php?username=$username";
			echo "<script language='javascript' type = 'text/javascript'>";
			echo "alert('Login success! Please click on the enter to continue:');location.href = '$url'";
			echo"</script>";
		}
	}
	else if($show ==2)
	{
		if(strlen($result)<80)
		{
			
			echo "register failed<br>";
			
		}
		else
		{
			echo 'register success<br>';
				
		}
	}
}

?>

</body>
</html>
